package com.chatai.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/test")
public class TestController {
    private static final Logger logger = LoggerFactory.getLogger(TestController.class);

    /**
     * 简单的测试端点，用于验证系统基本功能
     */
    @GetMapping("/health")
    public ResponseEntity<Map<String, String>> healthCheck() {
        try {
            logger.info("健康检查请求");
            Map<String, String> response = new HashMap<>();
            response.put("status", "UP");
            response.put("message", "系统运行正常");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("健康检查失败: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body(Map.of(
                    "status", "DOWN",
                    "error", e.getMessage()
            ));
        }
    }

    /**
     * 简单的POST测试端点
     */
    @PostMapping("/echo")
    public ResponseEntity<Map<String, Object>> echo(@RequestBody Map<String, Object> request) {
        try {
            logger.info("回显请求: {}", request);
            Map<String, Object> response = new HashMap<>();
            response.put("received", request);
            response.put("message", "请求已成功接收并回显");
            response.put("status", "success");
            return ResponseEntity.ok(response);
        } catch (Exception e) {
            logger.error("回显请求处理失败: {}", e.getMessage(), e);
            return ResponseEntity.status(500).body(Map.of(
                    "status", "error",
                    "error", e.getMessage()
            ));
        }
    }
}